from tkinter import *
from tkinter.messagebox import askokcancel


class Quitter(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        self.pack()
        # 组件
        widget = Button(self, text="退出", command=self.quit)
        widget.pack(side=LEFT, expand=YES, fill=BOTH)

    def quit(self):
        ans = askokcancel("确认退出", "您确认要退出吗？")
        if ans: Frame.quit(self)


fields = '姓名', '年龄', '职业'


def fetch(entries):
    for entry in entries:
        print('输入的文本是：', entry.get())


def make_form(root, fields):
    entries = []
    for field in fields:
        row = Frame(root)
        lab = Label(row, width=5, text=field)
        # 输入框
        ent = Entry(row)
        # 一行
        row.pack(side=TOP, fill=X)
        # 标签
        lab.pack(side=LEFT)
        ent.pack(side=RIGHT, expand=YES, fill=X)
        entries.append(ent)
    return entries


if __name__ == '__main__':
    root = Tk()
    ents = make_form(root, fields)
    root.bind('<Return>', (lambda event: fetch(ents)))
    Button(root, text='提交', command=(lambda: fetch(ents))).pack(side=LEFT)
    Quitter(root).pack(side=RIGHT)
    root.mainloop()
